package commands;


import util.Player;
import indexes.BattingIndex;
import indexes.PlayerIDIndex;
import indexes.PlayerNameIndex;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import util.BattingDatabaseFile;
import util.DatabaseFile;
import util.PlayerLogger;

import main.PlayerDB;

/**
 * This command is intended to retrieve player records
 * by name.
 * @author bfults (Brian Fults - 905084698)
 *
 */
public class IdentifyByNameCommand extends Command
{

    
    /**
     * Typical Constructor
     * @param args
     */
    public IdentifyByNameCommand(List<String> args)
    {
        super(args);
    }
    
    @Override
    /**
     * Arguments are in the form of first name, then last name
     * 
     */
    public boolean execute(
        PlayerIDIndex idIndex,
        PlayerNameIndex nameIndex,
        PlayerLogger logger,
        DatabaseFile database, BattingIndex battingIndex, BattingDatabaseFile battingDatabase ) throws IOException
    {
        logger.write(super.getDivider() + "\n");
        String key = getArguments().get(1) + ", " + getArguments().get(0);
        logger.write("Command: identify_by_name " + getArguments().get(0) + "\t" 
            + getArguments().get(1) + "\n\n");
        List<Long> offsets = nameIndex.get(key);
        if (offsets != null && offsets.size() > 0)
        {
            for (Long offset: offsets)
            {
                Player player = database.getPlayer( offset );
                logger.write("Player found.  Id: " + player.getPlayerId() + "\n");
            }
        }
        else
        {
            logger.write("No player found with name: " + key + "\n");
        }
        return true;
    }


}
